package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjSpareSpecInfoMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjSpareSpecInfoService;
import com.zb.utils.RandomUtil;
import com.zb.vo.GoodsDetailSpecSelectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 备件规格信息表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-15
 */
@Service
public class ZjSpareSpecInfoServiceImpl extends ServiceImpl<ZjSpareSpecInfoMapper, ZjSpareSpecInfo> implements ZjSpareSpecInfoService {
    @Resource
    private ZjSpareSpecInfoMapper spareSpecInfoMapper;
    // 新增、修改、删除规格
    @Override
    public void save(Long custAccId, Long spareId, List<GoodsSpecInfoVo> specVos, List<Long> delSepcIds) {
        // 校验规格名称是否重复
        Map<String, Boolean> specMap = new HashMap<>();
        Map<String, Boolean> codeMap = new HashMap<>();
        for (GoodsSpecInfoVo vo : specVos) {
            Assert.isTrue(null == specMap.get(vo.getName().trim()), "规格名称已重复："+ vo.getName());
            specMap.put(vo.getName().trim(), true);

            if (StringUtils.isNotBlank(vo.getCode())) {
                Assert.isTrue(null == codeMap.get(vo.getCode().trim()), "规格SKU码已重复："+ vo.getCode());
                codeMap.put(vo.getCode().trim(), true);
            }
        }
        if (CollectionUtil.isNotEmpty(delSepcIds)) {
            this.removeByIds(delSepcIds);
        }
        List<ZjSpareSpecInfo> specInfoList = this.getBySpareId(spareId);
        if (CollectionUtil.isNotEmpty(specInfoList)) {
            Long id = null;
            for (GoodsSpecInfoVo vo : specVos) {
                id = specInfoList.stream().filter(f -> Objects.equals(f.getName().trim(), vo.getName().trim())).map(ZjSpareSpecInfo::getId).findFirst().orElse(null);
                if (null == vo.getId() && null != id) vo.setId(id);
            }
        }
        List<ZjSpareSpecInfo> adds = new ArrayList<>();
        List<ZjSpareSpecInfo> updates = new ArrayList<>();
        List<Long> goodsSpecIds = new ArrayList<>();
        ZjSpareSpecInfo ps = null;
        for (GoodsSpecInfoVo vo : specVos) {
            ps = new ZjSpareSpecInfo();
            BeanUtils.copyProperties(vo, ps);
            if (null != ps.getId()) {
                updates.add(ps);
            } else {
                ps.setId(IdWorker.getId());
                ps.setCustAccId(custAccId);
                ps.setSpareId(spareId);
                ps.setPrice(null == ps.getPrice() ? new BigDecimal("0.0") : ps.getPrice());
                ps.setImage(null == ps.getImage() ? "" : ps.getImage());
                ps.setCode(StringUtils.isNotBlank(ps.getCode()) ? ps.getCode() : "PSKU" + RandomUtil.random10());
                adds.add(ps);
            }
            goodsSpecIds.add(ps.getId());
        }
        if (!adds.isEmpty()) {
            this.saveBatch(adds);
        }
        if (!updates.isEmpty()) {
            this.updateBatchById(updates);
        }
    }

    // 分页获取备件规格下拉数据列表
    @Override
    public Map<String, Object> pageSelect(Long spareId, SelectVo selectVo) {
        QueryWrapper<ZjSpareSpecInfo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "code", "image", "price", "over_price");
        wrapper.eq("spare_id", spareId);
        wrapper.eq("is_enabled", true);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjSpareSpecInfo> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectGoodsVo> voList = page.getRecords().stream().map(t -> {
            return new SelectGoodsVo(t.getId(), t.getName(), t.getImage()).setSpecSkuCode(t.getCode()).setPrice(t.getPrice()).setOverPrice(t.getOverPrice());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据备件ID获取规格列表
    @Override
    public Map<String, Object> getSpecList(Long spareId, GoodsDetailSpecSelectVo selectVo) {
        QueryWrapper<ZjSpareSpecInfo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "is_enabled AS enabled", "image", "price", "over_price", "warranty_day", "code");
        wrapper.eq("spare_id", spareId);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        if (StringUtils.isNotBlank(selectVo.getCode())) {
            wrapper.like("code", selectVo.getCode());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjSpareSpecInfo> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);

        List<GoodsSpecInfoVo> voList = page.getRecords().stream().map(vo -> {
            GoodsSpecInfoVo infoVo = new GoodsSpecInfoVo();
            BeanUtils.copyProperties(vo, infoVo);
            return infoVo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据ID查询规格信息
    @Override
    public List<ZjSpareSpecInfo> getByIds(List<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjSpareSpecInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        return this.list(wrapper);
    }

    // 根据备件ID删除备件规格信息
    @Override
    public void deleteBatchBySpareIds(List<Long> spareIds) {
        if (null == spareIds || spareIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjSpareSpecInfo> wrapper = new QueryWrapper<>();
        wrapper.in("spare_id", spareIds);
        this.remove(wrapper);
    }

    // 根据备件ID获取备件规格信息
    @Override
    public List<ZjSpareSpecInfo> getBySpareId(Long spareId) {
        QueryWrapper<ZjSpareSpecInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("spare_id", spareId);
        return this.list(wrapper);
    }

    // 分页获取工单配件规格下拉数据列表
    @Override
    public Map<String, Object> getOrderSpareSpecSelect(Long spareId, SelectVo selectVo) {
        PageUtil.setSelectPageLimit(selectVo);
        List<ZjSpareSpecInfo> specInfos = spareSpecInfoMapper.getOrderSpareSpecSelect(spareId, selectVo);
        List<SelectGoodsVo> voList = specInfos.stream().map(t -> {
            return new SelectGoodsVo(t.getId(), t.getName(), t.getImage()).setSpecSkuCode(t.getCode()).setPrice(t.getPrice()).setOverPrice(t.getOverPrice());
        }).toList();
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), spareSpecInfoMapper.getOrderSpareSpecSelectCount(spareId, selectVo), voList);
    }
}
